// 1.判断变量是数组类型
let arr = [];
console.log(arr instanceof Array);

// 2.写一个原型链继承的例子
function Element(ele) {
  this.ele = document.getElementById(ele);
}

Element.prototype.html = function(val) {
  var ele = this.ele;
  if (val) {
    ele.innerHTML = val;
    return this;
  } else {
    return ele.innerHTML;
  }
};

Element.prototype.on = function(type, fn) {
  var ele = this.ele;
  ele.addEventListener(type, fn);
  return this;
}

var element = new Element('main');

element.html('hello').on('click', function() {
  alert('handleClick');
});

// 3.描述new一个对象的过程
/**
 * 3.1 创建一个新对象
 * 3.2 this指向这个新对象
 * 3.3 执行代码给this赋值
 * 3.4 return this
 */
function Foo(name) {
  this.name = name;
  // return this;  // 本身会执行这一步
}
var f = new Foo('shiyanping');


// 4.写一个能遍历对象和数组的通用forEach函数
function forEach(obj, fn) {
  if (obj instanceof Array) {
    obj.forEach(function (item, index) {
      fn(index, item);
    })
  } else {
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        fn(key, obj[key]);
      }
    }
  }
}

var arr1 = [1, 2, 3, 4];
forEach(arr1, function (index, item) {
  console.log(index + ',' + item);
});

var obj = {
  x: 10,
  y: 20
};
forEach(obj, function (index, item) {
  console.log(index + ',' + item);
});

